package com.hellogood.yaohuo.client.bussiness.fanchang;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.bussiness.AbstractTab;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.services.FanchangService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;

public class FanchangTab extends AbstractTab {

	@Override
	public void initSearch() {
		searchTab = new FanchangSearchTab();
		searchTab.getSearchController().getListGrid().addDoubleClickHandler(new RecordDoubleClickHandler());
	}

	@Override
	public void initForm() {
		formTab = new FanchangFormTab(this);
	}

	private class RecordDoubleClickHandler implements DoubleClickHandler {

		@Override
		public void onDoubleClick(DoubleClickEvent event) {
			showFormDetail();
		}
	}

	private class FanchangAsyncCallback implements AsyncCallback<Form> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(Form result) {
			if (result != null) {
				formTab.getFormController().updateForm(result);
				tabSet.selectTab(formTab);
				if (result.getRzDate() != null && !result.getRzDate().equals("")) {
					showRzSuccessful();
				} else {
					showSaveSuccessful();
				}
			}
		}
	}

	@Override
	public void saveForm() {
		((FanchangFormController) formTab.getFormController()).saveForm();
	}

	@Override
	public void showFormDetail() {
		Record record = searchListGrid.getSelectedRecord();
		if (record != null) {
			String billNo = record.getAttribute("code");
			if (billNo != null) {
				FanchangService.Util.getInstance().getFormDetail(billNo, new FanchangAsyncCallback());
				showSaveSuccessful();
			}
		}
	}
}
